package org.finesys.common.core.util;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;

import cn.hutool.core.util.ArrayUtil;

public class PageUtils extends PageHelper {

    /**
     * 当前记录起始索引
     */
    public static final String PAGE_NUM = "pageNum";

    /**
     * 每页显示记录数
     */
    public static final String PAGE_SIZE = "pageSize";

    /**
     * 排序列
     */
    public static final String ORDER_BY_COLUMN = "orderByColumn";

    /**
     * 排序的方向 "desc" 或者 "asc".
     */
    public static final String IS_ASC = "isAsc";

    /**
     * 分页参数合理化
     */
    public static final String REASONABLE = "reasonable";

    public static void startPage() {
        int pageNum = ConvertUtil.toInt(ServletUtil.getParameter(PAGE_NUM), 1);
        int pageSize = ConvertUtil.toInt(ServletUtil.getParameter(PAGE_SIZE), 10);
        boolean reasonable = ServletUtil.getParameterToBool(REASONABLE);
        String orderByColumn = ServletUtil.getParameter(ORDER_BY_COLUMN, "");
        String isAsc = ServletUtil.getParameter(IS_ASC, "asc");
        String orderBy = getOrderBy(orderByColumn, isAsc);
        PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
    }

    /**
     * mybatisplus的分页参数
     */
    public static void startPage(Page<?> page) {
        int pageNum = (int) page.getCurrent();
        int pageSize = (int) page.getSize();
        boolean reasonable = page.optimizeCountSql();
        List<OrderItem> orderList = page.orders();
        String orderBy = getOrderBy(orderList);
        PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
    }

    private static String getOrderBy(String orderByColumn, String isAsc) {
        if (StrUtil.isEmpty(orderByColumn)) {
            return "";
        }
        return orderByColumn + " " + isAsc;
    }

    private static String getOrderBy(List<OrderItem> orderList) {
        if (ArrayUtil.isEmpty(orderList)) {
            return "";
        }
        String orderSql = orderList.stream().map(orderItem -> orderItem.getColumn() + " " + (orderItem.isAsc() == true ? "asc" : "desc")).
                collect(Collectors.joining(","));
        return orderSql;
    }
    /**
     * page不同实体类抓换方法
     * 
    */
    public static <T> Page<T> buildPage(Page<?> pageParam) {
        if (pageParam == null) {
            return null;
        }
        Page<T> page = new Page<>();
        page.setPages(pageParam.getPages());
        page.setCountId(pageParam.countId());
        page.setCurrent(pageParam.getCurrent());
        page.setMaxLimit(pageParam.maxLimit());
        page.setOptimizeCountSql(pageParam.optimizeCountSql());
        List<OrderItem> list = pageParam.orders();
        if (list != null) {
            page.setOrders(new ArrayList<OrderItem>(list));
        }
        page.setSearchCount(pageParam.searchCount());
        page.setSize(pageParam.getSize());
        page.setTotal(pageParam.getTotal());
        return page;
    }
}
